﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Configuration;
using System.ComponentModel.DataAnnotations;
using trangbaocao.Models;
using System.Collections.ObjectModel;
using System.Data.OracleClient;
namespace trangbaocao.Models
{
    public class PermissionModel
    {
        public int id { get; set; }
        [Required(ErrorMessage = "Tên quyền không được để trống!")]
        public string name { get; set; }
        public List<PermissionModel> getPermission()
        {
            List<PermissionModel> per = new List<PermissionModel>();
            OracleDataReader odr = General.Lib_GetDataReader("SELECT * FROM Permission order by id");
            while (odr.Read())
            {
                per.Add(new PermissionModel { id = Convert.ToInt32(odr[0].ToString()), name = odr[1].ToString() });
            }
            return per;
        }
        public void addPermission(PermissionModel per)
        {
            string sql = "INSERT INTO Permission";
            sql += " select nvl((select max(id)+1 from Permission),1),'" + per.name + "' from dual";
            General.Lib_ExecuteNonQuery(sql);
        }

        public void updatePermission(PermissionModel per)
        {
            string sql = "UPDATE Permission SET name='" + per.name + "' where id=" + per.id;
            General.Lib_ExecuteNonQuery(sql);
        }
        public void deletePermission(int id)
        {
            string sql = "DELETE FROM Permission WHERE id=" + id;
            General.Lib_ExecuteNonQuery(sql);
        }
        public int checkExist(PermissionModel per)
        {
            string sql = "SELECT * FROM Permission WHERE name='" + per.name + "'";
            return General.Lib_CheckExist(sql);
        }
        public int checkSubId(int id)
        {
            string sql = "SELECT * FROM Permission_Detail WHERE Permission_id='" + id + "'";
            return General.Lib_CheckExist(sql);
        }
    }
    public class PermissionDetailModel
    {
        public int id { get; set; }
        public int permission_id { get; set; }
        public int menu_id { get; set; }
        public List<PermissionDetailModel> getPermissionDetail(int id)
        {
            List<PermissionDetailModel> per = new List<PermissionDetailModel>();
            OracleDataReader odr = General.Lib_GetDataReader("SELECT * from permission_detail where permission_id=" + id);
            while (odr.Read())
                per.Add(new PermissionDetailModel { id = Convert.ToInt32(odr[0].ToString()), permission_id = Convert.ToInt32(odr[1].ToString()), menu_id = Convert.ToInt32(odr[2].ToString()) });
            return per;
        }
    }
    public class AccessMenuModel
    {
        public int id { get; set; }
        public string name { get; set; }
        public int parent_id { get; set; }
        public List<AccessMenuModel> getPermissionDetail()
        {
            List<AccessMenuModel> per = new List<AccessMenuModel>();
            OracleDataReader odr = General.Lib_GetDataReader("SELECT * from access_menu");
            while (odr.Read())
                per.Add(new AccessMenuModel { id = Convert.ToInt32(odr[0].ToString()), name = odr[1].ToString(), parent_id = Convert.ToInt32(odr[2].ToString()) });
            return per;
        }
    }
}